<?php

declare(strict_types=1);

namespace App\Controller\store;

use App\Middleware\User\StoreMiddleware;
use App\Request\Store\Rights\GroupAddRequest;
use App\Request\Store\Rights\MenuAddRequest;
use App\Request\Store\Rights\UserAddRequest;
use App\Request\Store\Rights\UserPolishRequest;
use App\Service\Store\RightsService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Annotation\Middleware;

/**
 *
 * 后台权限管理控制器
 * @Middleware(StoreMiddleware::class)
 * @AutoController()
 * @author 陈奕文
 * @package App\Controller\store
 */
class RightsController extends Controller
{

    /**
     * 权限服务
     * @Inject()
     * @var RightsService
     */
    private $service;

    /**
     * 查询后台用户
     */
    public function users()
    {
        $params = $this->request->all();

        $store_list = $this->service->userSearch($params);

        $this->success($store_list);
    }

    /**
     * 添加用户
     * @param UserAddRequest $request
     */
    public function userNew(UserAddRequest $request)
    {
        $params = $request->all();

        $result = $this->service->userAdd($params);

        $this->success($result);
    }


    /**
     * 查询后台用户组
     */
    public function groups()
    {
        $params = $this->request->all();
        $group_list = $this->service->groupSearch($params);
        $this->success($group_list);
    }

    /**
     * 后台用户组创建
     * @param GroupAddRequest $request
     */
    public function groupNew(GroupAddRequest $request)
    {
        $params = $request->all();
        $result = $this->service->groupAdd($params);
        $this->success($result);
    }

    /**
     * 获得所有后台的后端路由
     */
    public function allRoutes()
    {
        $this->success(getHttpStoreRoutes());
    }

    /**
     * 后台菜单创建
     * @param MenuAddRequest $request
     */
    public function menuNew(MenuAddRequest $request)
    {
        $params = $request->all();
        $result = $this->service->menuAdd($params);

        $this->success($result);
    }

    /**
     * 后台用户信息修改
     * @param UserPolishRequest $request
     */
    public function userPolish(UserPolishRequest $request)
    {
        $id = (int)$this->request->input("id");
        !$id && $this->error();

        $params = $request->all();
        $result = $this->service->userPolish($id, $params);
        $this->success($result);
    }

    /**
     * 菜单绑定单个路由
     */
    public function menuRoute()
    {
        $id = (int)$this->request->input("id");
        !$id && $this->error();

        $route = $this->request->input('route');
        !$route && $this->error("请传路由");

        $result = $this->service->menuRouteBind($id, $route);

        $this->success($result);
    }

    /**
     * 菜单信息修改
     */
    public function menuMultiRoutes()
    {
        $id = (int)$this->request->input("id");
        !$id && $this->error();

        $params = $this->request->all();

        $result = $this->service->menuPolishAndMultiBind($id, $params);

        $this->success($result);
    }

    /**
     * 获得单个菜单的详细信息
     */
    public function menuDetail()
    {
        $id = (int)$this->request->input("id");
        !$id && $this->error();
        $menu = $this->service->store_menu_dao->getMenuById($id);
        !$menu && $this->error("该菜单不存在");

        $this->success($menu);
    }

    /**
     * 获取所有菜单
     */
    public function menus()
    {
//        $first_menus = $this->service->store_menu_dao->getMenusByMap([
//            'level' => 1
//        ])->toArray();
//        $second_menus = $this->service->store_menu_dao->getMenusByMap([
//            'level' => 2,
//        ])->groupBy('parent_id')->toArray();
//
//        foreach ($first_menus as &$first_menu) {
//            $first_menu['children'] = $second_menus[$first_menu['id']]??[]; // 下级所有的菜单
//        }

        $menus = $this->service->getMenus();

        $this->success($menus);
    }


}
